﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using System.Collections;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using ASPNETVN.PORTAL.Components.UI.DataTypes;
using ASPNETVN.PORTAL.Components.Helpers;

namespace CDTM.Modules.ONLINE
{
    public enum ModulePages
    {
        Style1,
        Style2,
        Style3
    }

    public partial class Main : PortalModuleControl
    {
        public Main()
        {
            // Define an Array List
            ArrayList list1 = new ArrayList();

            // Sắp xếp theo cột.
            list1.Add(new ListItem("Style1", "Style1"));
            list1.Add(new ListItem("Style2", "Style2"));
            list1.Add(new ListItem("Style3", "Style3"));

            ModuleSettingItem settingItem = new ModuleSettingItem(new CustomListDataType(list1, "Text", "Value"));
            settingItem.Value = "Style1";
            settingItem.Name = "Kiểu hiển thị";
            settingItem.Description = "Kiểu hiển thị";
            settingItem.SortOrder = 1;
            this.BaseSettings.Add("DisplayStyle", settingItem);


            settingItem = new ModuleSettingItem(new StringDataType());
            settingItem.Value = "Default.aspx?tabid=" + Guid.Empty.ToString();
            settingItem.Name = "Link Management Page";
            settingItem.Description = "Link Management Page";
            settingItem.SortOrder = 1;
            this.BaseSettings.Add("ONLINE.LinkManagement", settingItem);
        }

        private void LoadChildControl()
        {
            const string basePath = "~/Modules/CDTM.Modules.ONLINE/";
            ModulePages page = (ModulePages)Enum.Parse(typeof(ModulePages), this.Settings["DisplayStyle"].ToString(), true);
            string src = string.Format("{0}Controls/{1}.ascx", basePath, page);
            ModulePage ctl = (ModulePage)this.LoadControl(src);
            ctl.ParentControl = this;
            ContentPane.Controls.Add(ctl);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.LoadChildControl();

            ModulePages page = (ModulePages)Enum.Parse(typeof(ModulePages), this.Settings["DisplayStyle"].ToString(), true);

            if (page == ModulePages.Style1)
            {
                this.ModuleTitle.AddUrl = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEditItem");
                this.ModuleTitle.ManagerUrl = HttpUrlBuilder.BuildAddEditUrl(ModuleID, 0, "Management");
            }
            else if (page == ModulePages.Style2) { }
            else if (page == ModulePages.Style3)
            {
                this.ModuleTitle.AddUrl = HttpUrlBuilder.BuildShareUrl(this.ModuleID, 0, "CategoryManager");
                this.ModuleTitle.ManagerUrl = GetUrlManager();
            }
        }

        public string GetUrlManager()
        {
            return this.Settings["ONLINE.LinkManagement"].ToString() != "Default.aspx?tabid=" + Guid.Empty.ToString() ?
                    this.Settings["ONLINE.LinkManagement"].ToString() : "#";
        }
    }
}